if (typeof ttDetectedMbSegmentSingle !== "undefined"){
	alert('You can only use this tool once after page load. Page would refresh now, and you can use it again.');
	location.reload();
}else{

	//Helper Functions
	function capitaliseFirstLetter(string){
		return string.charAt(0).toUpperCase() + string.slice(1);
	}

	var ttDetectedMbSegment = '',
		ttDetectedMbSegmentSingle=true; //assume we only have one segment, but check


	//Detect the Segment using the PromoBanner1_XXXX Mbox
	if ($('#mboxImported-default-PromoBanner1_Prem-0').length>0) {
		//Prem
		if (ttDetectedMbSegment==''){ //check if this is the first segment found
			ttDetectedMbSegmentSingle=true;
		}else{
			ttDetectedMbSegmentSingle=false;
		}
		ttDetectedMbSegment = 'Prem';
	}

	if ($('#mboxImported-default-PromoBanner1_Per-0').length>0) {
		//Per
		if (ttDetectedMbSegment==''){ //check if this is the first segment found
			ttDetectedMbSegmentSingle=true;
		}else{
			ttDetectedMbSegmentSingle=false;
		}
		ttDetectedMbSegment = 'Per';
	}

	if ($('#mboxImported-default-PromoBanner1_Bus-0').length>0) {
		//Per
		if (ttDetectedMbSegment==''){ //check if this is the first segment found
			ttDetectedMbSegmentSingle=true;
		}else{
			ttDetectedMbSegmentSingle=false;
		}
		ttDetectedMbSegment = 'Bus';
	}

	var ttSegmentChangeAnswer='';

	if (ttDetectedMbSegment!=''){
		ttSegmentChangeAnswer = prompt("The detected Segment on this page is: [" + ttDetectedMbSegment + "]. What do you want to change it to? Type in either of the three: Per, Prem or Bus.");
	}else{
		ttSegmentChangeAnswer = prompt("What do you want to change the segment to? Type in either of the three: Per, Prem or Bus.");
	}

	if (ttSegmentChangeAnswer===null || ttSegmentChangeAnswer==''){
		ttSegmentChangeAnswer = 'Per';
	}
	ttSegmentChangeAnswer=capitaliseFirstLetter(ttSegmentChangeAnswer);

	var ttAccTypeChangeAnswer = prompt("You can also force Account Type for the ProductPromoCarousel (or others using acc_type data) mboxes. What acc_type type do you want to use?  Type in one of these predefined values: PCC_BD,	PCC_CU, PCC_CA, PCC_LO_LN, PCC_IN, PCC_PE, PCC_SV, PCC_LI, PCC_NA, PCC_MO, PCC_SO, PCC_O, or if it's not on the list, just type in a different value:");
	if (ttAccTypeChangeAnswer===null || ttAccTypeChangeAnswer==''){
		ttAccTypeChangeAnswer = 'PCC_CU';
	}
	ttAccTypeChangeAnswer=ttAccTypeChangeAnswer.toUpperCase();


	var ttCustomSegment = ttSegmentChangeAnswer; //Per for example
	var ttCustomAccType = ttAccTypeChangeAnswer; //PCC_CU for example

	var ttCustomInstance = "0"; //set to 0 if you want it to auto increase i.e. 1,2,3 for each Product Promo Carousel, or to 1 or 2 or 3, if you want the same in all 3 locations

	var ttCustomInstanceAutoIncrease = false;
	if (ttCustomInstance==0){
		ttCustomInstanceAutoIncrease= true;
	}


	$('[id^="mboxImported-default-"]').each(function(e){

		var ttCurMBInnerHTML = $(this).html(),
			ttCurMBID = $(this).attr('id'),
			ttNewCurMBID = ttCurMBID.replace('mboxImported-default-','').replace('-0','').replace('Prem',ttCustomSegment);

		$('<div id="'+ttNewCurMBID+'_mbox_id'+'"></div>').insertAfter('#'+ttCurMBID); /* Create new Divs */
		$('#'+ttCurMBID).remove(); /* remove original; comment out if you need both */
		mboxDefine(ttNewCurMBID+'_mbox_id',ttNewCurMBID);

		/* Check whether it's a normal mbox or one with acc_type and acc_inst */

		if (ttCurMBID.indexOf('ProductPromoCarousel')!=-1){
			//Auto increase instance
			if (ttCustomInstanceAutoIncrease){
				ttCustomInstance++;
			}
			mboxUpdate(ttNewCurMBID,tc_extra_info,"cont_segment="+containerSegment, "acc_type="+ttCustomAccType, "acc_instance="+ttCustomInstance);
		}else{
			mboxUpdate(ttNewCurMBID,tc_extra_info,"cont_segment="+containerSegment);
		}

	});
}
